self.addEventListener("message",function(e){
    this.self.postMessage(wholeCrash(e.data.check,e.data.components));
});

// function 
function wholeCrash(checkComponents, components, xSort) {
    // let computedPoints = [];

    let detectorObj = {},detectorModels = [];
    for (let i = 0; i < checkComponents.length; i++) {
        let com1 = checkComponents[i];
        let rountComponents = components;
        for (let j = 0; j < rountComponents.length; j++) {
            let com2 = rountComponents[j];

            if(detectorObj[com2.model]){
                continue;
            }

            if (com1.content == com2.content) {
                continue;
            }

            if (crashDetector(com1, com2)) {
                detectorObj[com2.model] = true;
                detectorModels.push(com1.model,com2.model);
            }
        }
    }
    // console.log(computedPoints)
    return [...new Set(detectorModels)];
}

// 检测碰撞
function crashDetector(com1, com2) {
    let com1Points = getRectPoints(com1);

    let com2Points = getRectPoints(com2);
    // 判断点是否在矩形内
    let inset = isCrash(com1Points, com2Points);

    if (!inset) {
        inset = isCrash(com2Points, com1Points);
    }

    function isCrash(observePoints, points) {
        var flag = false;
        // 额外的点
        // let otherPoints = getRectSideCenterPoint(observePoints);
        observePoints.forEach(point => {
            // console.log(point,points);
            if (pointInRect(point, points)) {
                flag = true;
            }
        });
        return flag;
    }

    return inset;
}

// 获取矩形的顶点坐标
function getRectPoints(rectData) {

    let points = [
        [rectData.x - rectData.w / 2, rectData.y + rectData.h / 2],
        [rectData.x + rectData.w / 2, rectData.y + rectData.h / 2],
        [rectData.x + rectData.w / 2, rectData.y - rectData.h / 2],
        [rectData.x - rectData.w / 2, rectData.y - rectData.h / 2]
    ];

    if (rectData.rotate) {
        let center = [
            rectData.x,
            rectData.y
        ]
        points = getRotatePoints(points, center, rectData.rotate);
    }

    return points;
}

// 获取矩形边上中点的坐标
function getRectSideCenterPoint(points) {
    let sideCenterPoint = [];
    for (let i = 0; i < points; i++) {
        let linePoint1 = line[i];
        let linePoint2 = i == l - 1 ? line[0] : line[i + 1];

        sideCenterPoint.push([
            (linePoint1[0] + linePoint2[0]) / 2,
            (linePoint1[1] + linePoint2[1]) / 2
        ]);
    }
    return sideCenterPoint;
}

// 将一点绕另一点顺时针旋转
function getRotatePoint(point, center, rotate) {
    // 角度转换
    let angle = rotate * Math.PI / 180;

    return [
        (point[0] - center[0]) * Math.cos(angle) + (point[1] - center[1]) * Math.sin(angle) + center[0],
        - (point[0] - center[0]) * Math.sin(angle) + (point[1] - center[1]) * Math.cos(angle) + center[1]
    ]

    // return [
    //     parseFloat(((point[0] - center[0]) * Math.cos(angle) + (point[1] - center[1]) * Math.sin(angle) + center[0]).toFixed(4)),
    //     parseFloat((- (point[0] - center[0]) * Math.sin(angle) + (point[1] - center[1]) * Math.cos(angle) + center[1]).toFixed(4))
    // ]
}

// 将多个点转换
function getRotatePoints(points, center, rotate) {
    return points.map(point => getRotatePoint(point, center, rotate));
}

// 点是否在矩形内
function pointInRect(point, line) {
    var inside = false;
    for (let i = 0, l = line.length; i < l; i++) {
        let linePoint1 = line[i];
        let linePoint2 = i == l - 1 ? line[0] : line[i + 1];
        if (dotOnLine(point, linePoint1, linePoint2)) {
            return true;
        }
        let slope = (linePoint2[1] - linePoint1[1]) / (linePoint2[0] - linePoint1[0]);
        // 判断点是否在线段的x轴之间
        let betweenX = (linePoint1[0] < point[0] && linePoint2[0] > point[0]) || (linePoint1[0] > point[0] && linePoint2[0] < point[0]);
        // 以point作一条平行于Y轴的射线，判断射线与经过linePoint1和linePoint2两点的直线的交点,是否位于point的y轴的上方
        let intersect = point[1] < (slope * (point[0] - linePoint1[0]) + linePoint1[1]);

        if (betweenX && intersect) {
            inside = !inside;
        }
    }
    return inside;
}

// 判断点是否在线段上
function dotOnLine(dot, terminal1, terminal2) {
    // let defaults = {
    //     dot: [],
    //     terminal1: [],
    //     terminal2: []
    // };

    // Object.assign(defaults, coords);

    // let {dot, terminal1, terminal2} = defaults;
    let isBetweenX = false;
    let isBetweenY = false;

    if (terminal1[0] < terminal2[0]) {
        //x1 < x2
        if (dot[0] >= terminal1[0] && dot[0] <= terminal2[0]) {
            //dotX 在两端点横坐标之间
            isBetweenX = true;
        } else {
            //dotX 不在两端点横坐标之间
            isBetweenX = false;
        }
    } else {
        //x1 >= x2
        if (dot[0] >= terminal2[0] && dot[0] <= terminal1[0]) {
            //dotX 在两端点横坐标之间
            isBetweenX = true;
        } else {
            //dotX 不在两端点横坐标之间
            isBetweenX = false;
        }
    }

    if (terminal1[1] < terminal2[1]) {
        //y1 < y2
        if (dot[1] >= terminal1[1] && dot[1] <= terminal2[1]) {
            //dotY 在两端点横坐标之间
            isBetweenY = true;
        } else {
            //dotY 不在两端点横坐标之间
            isBetweenY = false;
        }
    } else {
        //y1 >= y2
        if (dot[1] >= terminal2[1] && dot[1] <= terminal1[1]) {
            //dotY 在两端点横坐标之间
            isBetweenY = true;
        } else {
            //dotY 不在两端点横坐标之间
            isBetweenY = false;
        }
    }

    if (isBetweenX && isBetweenY) {
        //dot 有可能在线段上
        let k = (terminal2[1] - terminal1[1]) / (terminal2[0] - terminal1[0]);
        let b = terminal1[1] - terminal1[0] * k;

        if (dot[1] == k * dot[0] + b) {
            //dot 在线段上
            return true;
        } else {
            //dot 不在线段上
            return false;
        }
    } else {
        //dot 没可能在线段上
        return false;
    }
};

// 点是否在矩形内plus
function isInPolygon(checkPoint, polygonPoints) {
    var counter = 0;
    var i;
    var xinters;
    var p1, p2;
    var pointCount = polygonPoints.length;
    p1 = polygonPoints[0];

    for (i = 1; i <= pointCount; i++) {
        p2 = polygonPoints[i % pointCount];
        if (
            checkPoint[0] > Math.min(p1[0], p2[0]) &&
            checkPoint[0] <= Math.max(p1[0], p2[0])
        ) {
            if (checkPoint[1] <= Math.max(p1[1], p2[1])) {
                if (p1[0] != p2[0]) {
                    xinters =
                        (checkPoint[0] - p1[0]) *
                        (p2[1] - p1[1]) /
                        (p2[0] - p1[0]) +
                        p1[1];
                    if (p1[1] == p2[1] || checkPoint[1] <= xinters) {
                        counter++;
                    }
                }
            }
        }
        p1 = p2;
    }
    if (counter % 2 == 0) {
        return false;
    } else {
        return true;
    }
}